package com.yksj.healthtalk.ui.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 找回密码
 * @author origin
 */
public class ResetPasswordUItwo extends BaseFragmentActivity implements OnClickListener {
	String mNumber;//多美号
	String mPhone;//手机号
	String mCode;//验证码
	EditText mNewPasText;
	EditText mConfirmText;
	Button mCodeButton;
	String valideCode;//验证码
	
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.reset_password_layout_two);
		initUI();
	}
	
	private void initUI(){
		initTitle();
		
		setBackBtn("返回",this);
		
		setTitle("找回密码");
		
		mCodeButton = (Button)findViewById(R.id.get_code);
		mConfirmText = (EditText)findViewById(R.id.confirm_txt);
		mNewPasText = (EditText)findViewById(R.id.newpasw_txt);
		//加软键盘监听
		mConfirmText.setImeOptions(EditorInfo.IME_ACTION_DONE);
		mConfirmText.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId==EditorInfo.IME_ACTION_DONE){
					onChangePassword();
				return true;
				}else{
				return false;	
				}
			}
		});
		
		findViewById(R.id.login_btn).setOnClickListener(this);
		
		TextView tv=(TextView) findViewById(R.id.duomeinumber);
		Intent intent=getIntent();
		mNumber=getIntent().getStringExtra("CUSTOMER_ACCOUNTS");
		mPhone=getIntent().getStringExtra("phone");
		mCode=getIntent().getStringExtra("code");
		tv.setText(mNumber);
	}
	
	
	private void onChangePassword(){
		if(mNewPasText.length() < 6){
			ToastUtil.showShort("您输入的密码应该是6-12位");
			return;
		}
		
		if(!mConfirmText.getEditableText().toString().equals(mNewPasText.getEditableText().toString())){
			mConfirmText.setError("两次密码填写不一样");
			return;
		}
		HttpRestClient.doHttpResetPaswd(mPhone,
				mCode,
				mNewPasText.getEditableText().toString(),
				new AsyncHttpResponseHandler(this){
				@Override
				public void onSuccess(int statusCode, String content) {
					if(!isFinishing()){
						if("1".equals(content)){
							ToastUtil.showToastPanl("修改成功");
							onBackPressed();
						}else{
							SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), content);
						}
					}
				}
		});
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.login_btn:
			onChangePassword();
			break;
		}
	}
}
